﻿module Role
{

	export class EditRoleModal
	{
		private modal: JQuery;
		public actionBoard = new Soict.Board();

		public parseData(data: any): void
		{
			$('#editmodel_name').val(data['name']);
			$('#editmodel_description').val(data['description']);

			var actions = data['Actions'];
			for (var i in actions)
			{
				this.actionBoard.add(actions[i].id, actions[i].description);
			}

		}

		public init()
		{
			this.actionBoard.init('actionBoard');
			this.modal = $('#editRoleModal');

		}

		public show()
		{
			this.modal["foundation"]('reveal', 'open');
		}

		public hide()
		{
			this.modal["foundation"]('reveal', 'close');
		}
	}

	export class RoleManager
	{
		private editModal = new EditRoleModal();

		private waitModal = new Modal.Wait();
		private usernameS: JQuery;
		private roleid: string;
		private errorModal = new Modal.Error();

		public init(): void
		{

			this.errorModal.init("Kết nối tới server bị ngắt, xin thử lại sau ít phút");
			this.editModal.init();
			this.waitModal.init();

			var thethis = this;

			$('#userAddActionBt').on('click', function ()
			{
				var myWindow = window.open("/Action", "", "width=800,height=600");


				myWindow.onunload = function ()
				{
					if (myWindow['pickedId'] == undefined) return;
					var id = myWindow['pickedId'];
					if (id == "") return;
					thethis.waitModal.show();

					$.post('/User/AddProject', { rid: id, aid: thethis.roleid }, function (data)
					{
						console.log(data);
						var json = JSON.parse(data);
						thethis.editModal.actionBoard.add(json.id, json.description);
						thethis.editModal.show();

					}).fail(function ()
						{
							thethis.waitModal.hide();
							thethis.errorModal.show();
						});

					console.log('id picked = ' + id);

				}


			});

			WindJS.wire(this.editModal.actionBoard, "onTagRemove", function (data)
			{
				console.debug(data);
				thethis.waitModal.show();
				$.post("/Role/RemoveAction", { aid: data.id, rid: thethis.roleid }, function (s:string)
				{
				
					if (s == '"Removed Action"')
					{
						thethis.editModal.actionBoard.remove(data.id);
						thethis.editModal.show();
					}
					else
					{
						thethis.errorModal.changeMessage('Không thể xóa được');
						thethis.errorModal.show();
					}
				}).fail(function ()
					{
						thethis.errorModal.changeMessage('Kết nối tới server bị ngắt, xin thử lại sau ít phút nữa');
						thethis.errorModal.show();
					});
				thethis.editModal.show();
			});

			$('.removeBt').bind('click', function (data)
			{

				var modelID = this.getAttribute["data-userid"];
			});


			$('.editBt').on('click', function (data)
			{

				var modelID = this.getAttribute("data-userid");
				thethis.roleid = modelID;
				thethis.waitModal.show();

				$.post("/Role/Get", { id: modelID }, function (data)
				{
					console.log(data);
					if (data == "") 
					{
						//theo lý thuyết là không bao giờ đi vào nhánh này
						console.error("wrong roleid");
						setTimeout(function ()
						{
							thethis.waitModal.hide();
						}, 1000);
					}
					var json = JSON.parse(data);

					thethis.editModal.parseData(json);
					thethis.editModal.show();
				}).fail(function (xhr, textStatus, errorThrown)
					{
						console.debug(xhr);
						setTimeout(function ()
						{
							thethis.waitModal.hide();
						}, 400);
					});;

			});



		}


	}



	export class PickRole
	{

		private pagi = new Pagination.Div();

		public init()
		{
			this.pagi.init();
			var thethis = this;

			WindJS.wire(this.pagi, 'onNext', function ()
			{
				thethis.pagi.setIndex(thethis.pagi.getIndex() + 1);
				$('#findBt').click();
			});


			WindJS.wire(this.pagi, 'onNav', function (data: any)
			{
				thethis.pagi.setIndex(data.index);
				$('#findBt').click();

			});

			WindJS.wire(this.pagi, 'onPrev', function ()
			{
				thethis.pagi.setIndex(thethis.pagi.getIndex() - 1);
				$('#findBt').click();
			});

			$('#findBt').on('click', function ()
			{
				window.location.href = "/Role?keyword=" + encodeURIComponent($('#findTb').val()) + "&page=" + encodeURIComponent($('#paginationPageTb').val()) + "&pagesize=30";
			});

			$('.pickBt').on('click', function (data)
			{
				var modelID = this.getAttribute("data-userid");
				window['pickedId'] = modelID;
				window.close();
			});

			$('.removeBt').on('click', function (data)
			{
				var modelID = this.getAttribute("data-userid");
				window.location.href = "Role/Delete?id=" + encodeURIComponent(modelID);
			});


		}



	}
}
var s01Role = new Role.PickRole();
var s01RoleM = new Role.RoleManager(); s01RoleM.init();
s01Role.init();

window['pickedId'] = "";